package operation;

/**
 * Creared with IntelliJ IDEA.
 * Description:
 * User:yxd
 * Date:2022-01-05
 * Time:2:08
 */
import book.Book;
import book.BookList;

public class DelOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入要删除的图书");
        String name = input.nextLine();
        int size = bookList.getUsedSize();
        int index = 0;//存储找到的下标
        int i = 0;
        for (; i < size - 1; i++) {
            Book book = bookList.getBook(i);//寻找所有书
            if(name.equals(book.getName())){
                index = i;//找到的要删除的下标
                break;
            }
        }
        if(i >= size){
            System.out.println("没有你要删除的书");
            return;
        }
        for (int j = 0; j < size - 1; j++) {
            Book book = bookList.getBook(j + 1);//获得j后面的图书
            bookList.setBooks(j,book);//让j位置的书被j+1位置的书所覆盖
        }
        bookList.setBooks(size,null);//让最后一本书置空
        bookList.setUsedSize(size - 1);//让有效元素减一
    }
}

